package test;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import com.brother.alarm.AlarmObserverImpl;
import com.brother.model.AlarmType;
import com.brother.model.RealTimeData;
import com.brother.service.RealDataService;

/**
 * @author ling
 *
 */
public class SimulateRealData {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		RealDataService rs = new RealDataService();
		RealTimeData data = new RealTimeData();
		data.setOrient("0");
		
//		String lat = "4046.688400";
//		String lng = "0741.878600";
		float lat1 = 41f;
		float lat2 = 46.688400f;
		float lng1 = 74f;
		float lng2 = 30.878600f;
//		data.setOrig_lat("4046.688400");
//		data.setOrig_lng("0741.878600");
		data.setOrig_lat(Integer.toString((int)lat1)+Float.toString(Math.abs(lat2)));
		DecimalFormat nf =new DecimalFormat("000");
		
		data.setOrig_lng(nf.format(Math.abs(lng1))+Float.toString(Math.abs(lng2)));
		data.setLng(-(lng1+(lng2 / 60f)));
		data.setTrackerid("7d79a5b9-2cd3-4e23-b3d1-a0ab9c99eac6");
		data.setState(AlarmType.GPS_Pos.toString());
		data.setSignal("ok");
		data.setLatOrient("N");
		data.setLngOrient("W");
		data.setDistance("0");
		
		AlarmObserverImpl alo = new AlarmObserverImpl();
		data.setAlarmObserver(alo);
		
		
		for(int i = 0; i < 1500; i++){
			if(i < 200){
				float[] lat = addLatSix(lat1,lat2, 0.0311f);
				data.setLat(lat[0]+lat[1]/60f);
				Date d = new Date();
				data.setTraceDateTime(d);		
				lat1 = lat[0];
				lat2 = lat[1];
				data.setOrig_lat(Integer.toString((int)lat1)+Float.toString(Math.abs(lat2)));
				System.out.println("lat1:"+lat1+"   lat2:"+lat2+"   lat:"+data.getLat()+"  orig_lat:"
						+data.getOrig_lat()+"  orig_lng:"+data.getOrig_lng()+"  index:"+i);
			}else{
				
				float[] lng = addLatSix(lng1,lng2, 0.0311f);
				data.setLng(-(lng[0]+lng[1]/60f));
				Date d = new Date();
				data.setTraceDateTime(d);		
				lng1 = lng[0];
				lng2 = lng[1];
				data.setOrig_lng(nf.format(Math.abs(lng1))+Float.toString(Math.abs(lng2)));
				System.out.println("lng1:"+lng1+"   lng2:"+lng2+"   lng:"+data.getLng()+"  orig_lng:"
						+data.getOrig_lng()+"  index:"+i);
			}
			if(i % 10 == 0)
				data.setSpeed(40);
			else
				data.setSpeed(40);
			data.ckeckTriggerredAlarm();
			rs.insertData(data);
			Thread.sleep(1000);
		}
//		for(int i = 0; i < 100; i++){
//			lat1 = lat1 + 0.0181f;
//			data.setLat(toAl(lat1));
//			Date d = new Date();
//			data.setTraceDateTime(d);
//			data.setOrig_lat(Float.toString(lat1));
//			System.out.println("lat:"+data.getLat()+"   lng:"+data.getLng()+"  index:"+i);
//			//rs.insertData(data);
//			//Thread.sleep(1000);
//		}
		
		
		
	}

	/**将分制转换成十进制，前面两位是度数，后面是分
	 * @param lat1
	 * @return
	 */
	private static float toAl(float lat1) {
		float a = (int)(lat1 / 100f);
		float b = (lat1 - a * 100f) / 60f; 
		return a+b;
	}

	/*
	 * lat1纬度整数，lat2纬度分数
	 * */
	private static float[] addLatSix(float lat1, float lat2, float amount) {
		float[] r = new float[2];
			lat2 = lat2 + amount;
			lat1 = (int)(lat2 / 60f) + lat1;
			
			lat2 = (lat2 - (int)(lat2 / 60f)*60f);
			r[0] = lat1;
			r[1] = lat2;
		return r;
	}

}
